package cc.ug_project.rollcall.Activities

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import cc.ug_project.rollcall.Bussness.DataProviders.UserDataProvider
import cc.ug_project.rollcall.Bussness.UserSignInState
import cc.ug_project.rollcall.Bussness.UserViewModel
import cc.ug_project.rollcall.R
import cc.ug_project.rollcall.globalUser
import com.pawegio.kandroid.textWatcher
import com.ug_project.alert.alert
import com.ug_project.data.Preference
import com.ug_project.views.click
import com.ug_project.views.content
import kotlinx.android.synthetic.main.activity_login.*


class LoginActivity : AppCompatActivity() {
    var localName by Preference(this, "LocalUserName", "")

    val userDataProvider by lazy { UserDataProvider() }
    val viewModel by lazy { UserViewModel(userDataProvider) }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)

        if(Build.VERSION.SDK_INT>=21){
            supportActionBar?.setElevation(0f);
        }

        viewModel.nameDelegate.apply {
            get = { UserName.content }
            set = { if (UserName.content != it) UserName.content = it }
        }
        viewModel.pwdDelegate.apply {
            get = { UserPwd.content }
            set = { if (UserPwd.content != it) UserPwd.content = it }
        }
        viewModel.canSignInDelegate.apply {
            set = {
                SignIn.isEnabled = it
            }
        }
        viewModel.exceptionDelegate.apply {
            set = {
                alert(it.toString())
            }
        }
        viewModel.userDelegate.apply {
            set = {
                globalUser = it
                goActivity()
            }
        }
        viewModel.notFoundDelegate.apply {
            set = {
                alert(getString(R.string.incorrent_username_or_password))
            }
        }

        viewModel.signInStateDelegate.apply {
            set = {
                when (it) {
                    UserSignInState.NotStarted -> {
                        login_form.visibility = View.VISIBLE
                        login_progress.visibility = View.GONE
                    }

                    UserSignInState.SigningIn -> {
                        login_form.visibility = View.GONE
                        login_progress.visibility = View.VISIBLE
                    }
                }
            }
        }

        UserName.textWatcher {
            afterTextChanged { viewModel.name = it.toString() }
        }

        UserPwd.textWatcher {
            afterTextChanged { viewModel.password = it.toString() }
        }

        SignIn.click {
            viewModel.signIn()
        }

        viewModel.name = localName
    }

    fun goActivity(){
        localName = viewModel.name

        startActivity(Intent(this, MainActivity::class.java))
        this.finish()
    }
}